package com.linjiu.edit;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;


/**
 * Created by alice on 2017/10/25.
 */
public class ViewUtil {

	public static int dp2px(Context context, float dp) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dp * scale + 0.5f);
	}

	public static int px2dp(Context context, float px) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (px / scale + 0.5f);
	}


	public static DisplayMetrics getScreenSize(Activity activity){
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm;
	}

	/**
	 * 将本地资源图片大小缩放
	 * @param resId
	 * @param w
	 * @param h
	 * @return
	 */
	public static Drawable zoomImage(Resources res,int resId, int w, int h){
		Bitmap oldBmp = BitmapFactory.decodeResource(res, resId);
		Bitmap newBmp = Bitmap.createScaledBitmap(oldBmp,w, h, true);
		Drawable drawable = new BitmapDrawable(res, newBmp);
		return drawable;
	}



	static Drawable zoomDrawable(Drawable drawable, int w, int h)
	{
		int width = drawable.getIntrinsicWidth();
		int height= drawable.getIntrinsicHeight();
		Bitmap oldbmp = drawableToBitmap(drawable); // drawable 转换成 bitmap
		Matrix matrix = new Matrix();   // 创建操作图片用的 Matrix 对象
		float scaleWidth = ((float)w / width);   // 计算缩放比例
		float scaleHeight = ((float)h / height);
		matrix.postScale(scaleWidth, scaleHeight);         // 设置缩放比例
		Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);       // 建立新的 bitmap ，其内容是对原 bitmap 的缩放后的图
		return new BitmapDrawable(newbmp);       // 把 bitmap 转换成 drawable 并返回
	}


	static Bitmap drawableToBitmap(Drawable drawable) // drawable 转换成 bitmap
	{
		int width = drawable.getIntrinsicWidth();   // 取 drawable 的长宽
		int height = drawable.getIntrinsicHeight();
		Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565;         // 取 drawable 的颜色格式
		Bitmap bitmap = Bitmap.createBitmap(width, height, config);     // 建立对应 bitmap
		Canvas canvas = new Canvas(bitmap);         // 建立对应 bitmap 的画布
		drawable.setBounds(0, 0, width, height);
		drawable.draw(canvas);      // 把 drawable 内容画到画布中
		return bitmap;
	}
}
